package com.monitorRoutine.monitorRoutineServer.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.monitorRoutine.monitorRoutineServer.entity.ControlCommand;
import com.monitorRoutine.monitorRoutineServer.entity.ControlCommandFile;
import com.monitorRoutine.monitorRoutineServer.entity.ControlCommandSet;
import com.monitorRoutine.monitorRoutineServer.mapper.ControlCommandMapper;
import com.monitorRoutine.monitorRoutineServer.service.ControlCommandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ControlCommandServiceImpl extends ServiceImpl<ControlCommandMapper, ControlCommand> implements ControlCommandService {
    @Autowired
    ControlCommandMapper controlCommandMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Value("${file.savePath}")
    String SAVE_PATH;
    @Override
    public boolean saveControlCommand(ControlCommandSet controlCommandSet) {
        for (String s : controlCommandSet.getNameList()) {
            ControlCommand controlCommand = new ControlCommand(s, controlCommandSet.getComman(),controlCommandSet.getCommandDetails());
            this.save(controlCommand);
        }
        return true;
    }

    @Override
    public boolean saveControlCommand(ControlCommandSet controlCommandSet,String fileName,String type){


        if (type.equals("1")){
            ControlCommandFile controlCommandFile = new ControlCommandFile(fileName, controlCommandSet.getCommandDetails());
            controlCommandSet.setCommandDetails(JSON.toJSONString(controlCommandFile));
            saveControlCommand(controlCommandSet);
        }else if ("2".equals(type)){

            Map<String, String> map = JSON.parseObject(controlCommandSet.getCommandDetails(), new TypeReference<Map<String, String>>() {});
            for (String s : controlCommandSet.getNameList()) {
                ControlCommand controlCommand = new ControlCommand(s, controlCommandSet.getComman(),map.get(s));
                this.save(controlCommand);
            }
        }

        return false;
    }

    @Override
    public void scanCommandDatabase() {
        List<ControlCommand> controlCommand = controlCommandMapper.selectExecute("0");
    // 使用管道执行多个命令
        List<Object> results = redisTemplate.execute(new SessionCallback<List<Object>>() {
            @Override
            public List<Object> execute(RedisOperations operations) throws DataAccessException {
                // 开始管道
                operations.multi();

                // 在这里添加你的命令
                for (ControlCommand command : controlCommand) {

                    redisTemplate.opsForList().rightPush(command.getName(), JSON.toJSONString(command));
                    command.setExecute("1");
                    command.setExecutiontime(new Date());
                }

                // 执行管道中的所有命令
                return operations.exec();
            }
        });

        this.updateBatchById(controlCommand);
    }


}
